package com.dongdongshop.controller;

import com.dongdongshop.page.PageBean;
import com.dongdongshop.util.Result;
import com.dongdongshop.enums.ResultEnum;
import com.dongdongshop.pojo.TbBrand;
import com.dongdongshop.service.BrandService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("brand")
public class BrandController {

    @DubboReference
    private BrandService bs;

    @RequestMapping("show")
    public String show() {
        return "admin/brand";
    }

    @RequestMapping("getAll")
    @ResponseBody
    public Result<TbBrand> getAll(@RequestParam(defaultValue = "1") Integer pageNum,
                                  @RequestParam(defaultValue = "5") Integer pageSize) {
        try {
            PageBean all = bs.getAll(pageNum, pageSize);
            return Result.OK().setData(all);
        } catch (Exception e) {
            return Result.response(ResultEnum.SELECT_MESSAGE_ERROE);
        }
    }

    @RequestMapping("getSelect")
    @ResponseBody
    public Result getSelect() {
        try {
            List<TbBrand> all = bs.ListBrands();
            return Result.OK().setData(all);
        } catch (Exception e) {
            return Result.response(ResultEnum.ERROR);
        }
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbBrand b) {
        try {
            bs.insert(b);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.ADD_MESSAGE_ERROE);
        }
    }

    @RequestMapping("getById/{id}")
    @ResponseBody
    public Result getById(@PathVariable("id") Long id) {
        try {
            TbBrand byId = bs.getById(id);
            return Result.OK().setData(byId);
        } catch (Exception e) {
            return Result.response(ResultEnum.GETBYID_ERROR);
        }
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbBrand b) {
        try {
            bs.update(b);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.UPDATE_MESSAGE_ERROE);
        }
    }

    @RequestMapping("deleteCheck")
    @ResponseBody
    public Result deleteCheck(Long[] ids) {
        try {
            bs.deleteCheck(ids);
            return Result.OK();
        } catch (Exception e) {
            return Result.response(ResultEnum.DELETE_MESSAGE_ERROE);
        }
    }
}
